﻿using MvcApplication;
using MvcApplication.service;
using MvcEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication4MVC.request;

namespace WebApplication4MVC.Controllers
{

    /*ORM Object Relational  Mapping
     * 
     */
    public class HomeController : CommonController
    {
        DemoService userDemo = new DemoService();
        public ActionResult Index()
        {
            // temp.addDemo();

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult LeanJson(LeanJsonRequest request)
        {
            return Json(request, JsonRequestBehavior.AllowGet);
        }

        public ActionResult register()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult getuserlist()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult getulist(querylist request)
        {
            var result = userDemo.GetList(request.UserName,request.UserPhone, out int tatol,request.PageSize,request.PageNum);
            return Json(new { total = tatol, data = result });
        }

        public ActionResult submitReg(RegisterRequest request)
        {

            var du = new DemoUser();
            du.UserName = request.UserName;
            du.UserPassword = request.UserPassword;
            du.UserPhone = request.UserPhone;
            
            //string str = Session["temp"]?.ToString();
            //HttpCookie httpCookie = new HttpCookie("temp");


            userDemo.addDemo(du);

            var obj = new { code = 200, msg = "ok" };
            return Json(obj);
        }

        public ActionResult MyInfo() {
            UserInfoModel loginser = Session["LoginUserInfo"] as UserInfoModel;
            if (loginser==null)
            {
                return RedirectToRoute(new { controller = "Login", action = "Index" });
            }
            ViewBag.UserInfo = loginser;
            return View();
        }

        public ActionResult GetUserInfo() {
            var reslt = new ResponseReslut();
            UserInfoModel loginser = Session["LoginUserInfo"] as UserInfoModel;
            if (loginser == null)
            {
                reslt.code = 401;
                reslt.msg = "授权失败，请登录！";
            }
            else {
                reslt.code = 200;
                reslt.msg = "ok";
                reslt.data = loginser;
            }
            return Json(reslt);
        }
    }
}